package com.dqy.twserver.server.handle.ehandle.mutil;

import com.dqy.twserver.server.dto.GameDto;
import com.dqy.twserver.server.handle.ehandle.GameEventHandle;
import com.dqy.twserver.server.handle.ehandle.GameEventSendMsgHandle;
import com.dqy.twserver.server.support.Constans;
import com.dqy.twserver.server.support.MsgPush;
import com.dqy.twserver.server.support.Player;
import com.dqy.twserver.server.support.SupportMap;
import com.dqy.twserver.server.support.data.RoomInfo;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class GameEventOutRoomHandle implements GameEventHandle,IRoom {
    static Logger logger = LoggerFactory.getLogger(GameEventOutRoomHandle.class);
    @Override
    public void handle(GameDto req, NioSocketChannel channel) throws Exception {
        GameDto gameDto=new GameDto();
        logger.info("[GameEventOutRoomHandle] start");

        String roomId = req.getRoomId();
        String playerName = req.getPlayerName();
        int rid=roomIdToRid(roomId);
        RoomInfo roomInfo = SupportMap.roomMap.get(rid);
        logger.info("[GameEventOutRoomHandle] playerName={}，rid={}",playerName,rid);

        if(roomInfo==null){
            logger.info("[GameEventOutRoomHandle] 房间已经不存在");
            gameDto.setSts(400);
            gameDto.setInfo("房间已经不存在");
            channel.writeAndFlush(gameDto);
            return;
        }

        if("1".equals(roomInfo.getRoomSts())){
            logger.info("[GameEventOutRoomHandle] 游戏已开始");
            gameDto.setSts(400);
            gameDto.setInfo("游戏已开始");
            channel.writeAndFlush(gameDto);
            return;
        }

        Player player = SupportMap.playerMap.get(playerName);
        if(!roomInfo.getPlayerList().contains(player)){
            logger.info("[GameEventOutRoomHandle] 你不在该房间");
            gameDto.setSts(400);
            gameDto.setInfo("你不在该房间");
            channel.writeAndFlush(gameDto);
            return;
        }

        List<Player> playerList = roomInfo.getPlayerList();
        //判断是不是房主退出
        if(playerList.get(0).getPlayerName().equals(playerName)){
            //房主退出，直接解散房间
            MsgPush.sendMsg(playerList,"房间已被解散 !", "info",Constans.EVENT_TYPE_SEND_ROOM_IS_CLOSE); //发送关闭房间通知
            SupportMap.roomMap.remove(rid);
        }else {
            playerList.remove(player);
            logger.info("[GameEventOutRoomHandle] {}退出房间",playerName);

            String roomListInfoStr = getRoomInfo(playerList);
            logger.info("[GameEventOutRoomHandle] roomListInfoStr={}",roomListInfoStr);
            String msgBody=String.format("[系统通知]:%s退出房间",playerName);
            MsgPush.sendMsg(playerList,msgBody,"info", Constans.EVENT_TYPE_SEND_ALL_MSG);
            MsgPush.sendMsg(playerList,roomListInfoStr, "roomListStr",Constans.EVENT_TYPE_SEND_ROOM_INFO); //重新推送房间信息
        }

        gameDto.setSts(200);
        gameDto.setInfo("ok");
        channel.writeAndFlush(gameDto);

        logger.info("[GameEventOutRoomHandle] start");
    }
}
